//
//  ECAddressChooseTransitioner.m
//  ECard
//
//  Created by 齐焕 on 2018/9/4.
//  Copyright © 2018年 平安银行-智慧城市项目组. All rights reserved.
//

#import "ECAddressChooseTransitioner.h"
#import "ECAddressPresentationController.h"

@implementation ECAddressChooseTransitioningDelegate

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source {
    
    return [[ECAddressPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    ECAddressChooseAnimatedTransitioning *animationController = [[ECAddressChooseAnimatedTransitioning alloc] init];
    [animationController setIsPresentation:YES];
    
    return animationController;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    ECAddressChooseAnimatedTransitioning *animationController = [[ECAddressChooseAnimatedTransitioning alloc] init];
    [animationController setIsPresentation:NO];
    
    return animationController;
}
@end

@implementation ECAddressChooseAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.5;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *fromView = fromViewController.view;
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toView = toViewController.view;
    
    UIView *containerView = transitionContext.containerView;
    
    BOOL isPresentation = self.isPresentation;
    
    if (isPresentation) {
        [containerView addSubview:toView];
    }
    
    UIViewController *animatingViewController = isPresentation ? toViewController : fromViewController;
    UIView *animatingView = animatingViewController.view;
    
    
    
    CGRect appearedFrame = [transitionContext finalFrameForViewController:animatingViewController];
    CGRect dismissedFrame = appearedFrame;
    dismissedFrame.origin.y += dismissedFrame.size.height;
    
    if (isPresentation) {
        //dismissedFrame.origin.x -= dismissedFrame.size.width;
      
        
    } else {
        //dismissedFrame.origin.x += dismissedFrame.size.width;
   
    }
    
    
    CGRect initialFrame = isPresentation ? dismissedFrame : appearedFrame;
    CGRect finalFrame = isPresentation ? appearedFrame : dismissedFrame;
    
    [animatingView setFrame:initialFrame];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                          delay:0
         usingSpringWithDamping:300.0
          initialSpringVelocity:5.0
                        options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [animatingView setFrame:finalFrame];
                     }
                     completion:^(BOOL finished){
                         if (![self isPresentation]) {
                             [fromView removeFromSuperview];
                         }
                         [transitionContext completeTransition:YES];
                     }];
}

@end



